This tutorial shows how to use .gesture( TapGesture() ).
Method onEnded() accepts Closure as its last Parameter which is why Closure can be declared outside Parameters List.
In the below example circle changes color when you tap on it.
TapGesture().onEnded()
struct ContentView: View {
@State var color = true
var body: some View {
Circle()
.fill(color ? Color.green : Color.red)
.frame(width: 50, height: 50)
.gesture( TapGesture()
.onEnded {
self.color.toggle()
}
)
}
}
Initial color Tap to change color